import cairo
import os
from PIL import Image
from Simulinux import config
from Simulinux.Views.View import View
from Simulinux.Views.PinView import *

__all__ = ['GenericBlockView', 'TEXT_VPADDING', 'PIXMAP_SIZE']

TEXT_VPADDING = 5
PIXMAP_SIZE = (40,40)

class GenericBlockView(View):
    type = 'block'
    def __init__(self, block, parent_view):
        View.__init__(self)
        self._parent_view = parent_view
        self.block = block
        self.pinviews = []
        self.width = 40
        self.height = 40
        self.__position_backup = ()
    def prepare_move(self, newx, newy):
        self.__newx_candidate = newx
        self.__newy_candidate = newy
    def move(self):
        self.block.posx = self.__newx_candidate
        self.block.posy = self.__newy_candidate
        self.__newx_candidate = None
        self.__newy_candidate = None
    def save_current_position(self):
        self._position_backup = (self.block.posx, self.block.posy)
    def is_my_region(self, posx, posy):
        if (posx >= self.block.posx) and (posx <= self.block.posx + self.width) and \
           (posy >= self.block.posy) and (posy <= self.block.posy + self.height):
            return True
        else:
            return False
    def contains_my_region(self, initial, final):
        if initial[0] < final[0]:
            x0 = initial[0]
            x1 = final[0]
        else:
            x0 = final[0]
            x1 = initial[0]
        if initial[1] < final[1]:
            y0 = initial[1]
            y1 = final[1]
        else:
            y0 = final[1]
            y1 = initial[1]
        if (self.block.posx > x0) and (self.block.posx + self.width < x1) and \
           (self.block.posy > y0) and (self.block.posy + self.height < y1):
            return True
        else:
            return False
    def get_pin_view(self, x, y):
        for p in self.pinviews:
            if p.is_available() and p.is_my_region(x, y):
                return p
    def generate_pixmap(self, name):
        self.text = ""
        self.posx = self.posy = 0
        filename = os.path.join(config.PARTLIBPNG_DIR, name)
        filename += '.png'
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.width, self.height)
        ctx = cairo.Context(surface)
        self.draw(ctx, False)
        surface.write_to_png(filename)
        surface.finish()
        im = Image.open(filename)
        im.thumbnail(PIXMAP_SIZE, Image.ANTIALIAS)
        im.save(filename, 'PNG')
    def _create_single_output_pin_view(self, index):
        newpinview = PinView(
            self.block.pins[index],
            PIN_HORIZONTAL,
            self.width, 
            self.height/2, 
            self.width + PIN_LENGTH,
            self.height/2)
        self.pinviews.append(newpinview)
    def _create_single_input_pin_view(self, index):
        newpinview = PinView(
            self.block.pins[index],
            PIN_VERTICAL,
            -PIN_LENGTH, 
            self.height/2, 
            0,
            self.height/2)
        self.pinviews.append(newpinview)
    def _draw_pins(self, context):
        for p in self.pinviews:
            p.draw(context, self.block.posx, self.block.posy)